<template>
	<view class="Shell">
		<!-- 弹窗提示 -->
		<popWarn :info="info" :warningb="warningb"></popWarn>
	</view>
</template>

<script>
	// 引入底部组件
	var timer;
	const app = getApp();
	export default {
		data() {
			return {
				globalData: getApp().globalData,
				// 接口数据
				// 辅助数据
				fileUrl: '',
				// 组件数据
				warningb: false,
				info: '',
				// 布尔变量
				backb: false,
				// 兼容项
				platForm: app.globalData.platForm,
				systemInfo: '',
			}
		},
		onLoad(options) {
			//页面路径
			uni.showLoading({
				title: '文件下载中...'
			});
			this.systemInfo = uni.getSystemInfoSync().platform;
			this.fileUrl = decodeURIComponent(options.url);
			this.downLoadFn();
		},
		onShow() {
			if (this.backb) {
				this.backFn()
			}
		},
		methods: {
			// 方法调用区
			backFn() { //后退函数
				app.backFn();
			},
			warnFn(timeout = 2000) { //弹窗提醒
				this.warningb = true;
				clearTimeout(timer);
				timer = setTimeout(res => {
					this.warningb = false;
				}, timeout);
			},
			downLoadFn() {
				uni.downloadFile({ //下载远程文件
					url: this.fileUrl,
					success: res => {
						let tempFilePath = res.tempFilePath; //临时路径
						uni.hideLoading();
						uni.showModal({
							title: '下载成功！',
							content: `打开文件之后，点击右上角按钮${this.systemInfo=='ios'?'选择“用其他应用打开”或“转发给朋友”进行保存':'保存到手机'}`,
							confirmText: '打开文件',
							cancelText: '取消保存',
							success: operate => {
								if (operate.confirm) {
									uni.openDocument({
										showMenu: true, //关键点,可以转发到微信
										filePath: res.tempFilePath
									});
									this.backb = true;
								} else if (operate.cancel) {
									this.backFn()
								}
							}
						});
					},
					fail: res => {
						uni.showModal({
							title: '下载失败！',
							content: `失败原因：${res.errMsg}`,
							cancelText: '返回',
							success: operate => {
								if (operate.confirm || operate.cancel) {
									this.backFn()
								}
							}
						});
					}
				})
			}
		},
		watch: {
			info(newVal, oldVal) {
				if (newVal != '') {
					this.warnFn();
					setTimeout(res => {
						this.info = '';
					}, 2200)
				}
			}
		},
	}
</script>